<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 *  User_group Model Class
 *
 */
class  Province_model extends CI_Model
{
	var $id  ;

        var $id_country;

        var $vi_name;

        var $en_name;

	var $abbreviation;

	var $active;
        
        var $position;

        function __construct() {
            parent::__construct();
        }

	function insert($data){

		$sql = "INSERT INTO `gtb_province` " .
				"(" .
				"`id_country`, `vi_name`, `en_name`, `abbreviation`, `active`, `position`)" .
				" VALUES ";

		$sql .= "(" .
			intval($data['id_country']).", " .
                        $this->db->escape($data['vi_name']).", " .
                        $this->db->escape($data['en_name']).", " .
                        $this->db->escape($data['abbreviation']).", " .
                        intval($data['active']).", " .
                        intval($data['position']).
		")";

		$this->db->query($sql);
		return $this->db->insert_id();

	}

	function update($data){
		 $sql = "UPDATE gtb_province
			SET ".
                         " id_country = " .  $this->db->escape($data['id_country']) . "," .
			 " vi_name = " .  $this->db->escape($data['vi_name']) . "," .
                         " en_name = " .  $this->db->escape($data['en_name']) . "," .
                         " abbreviation = " .  $this->db->escape($data['abbreviation']) . "," .
                         " active = " .  $this->db->escape($data['active']) . "," .
			 " position = " .  intval($data['position']) .
                         " WHERE id = " .intval($data['id']);
		$this->db->query($sql);
		return $this->db->affected_rows();
	}

	function delete($id){
		// Kiem tra trc khi xoa, neu co record tham chieu thi ko cho xoa.
		// .......... chua lam

		if(!is_array($id)) $id = array($id);
		$ids = implode(',', $id);
		$sql = "DELETE FROM gtb_province WHERE id IN ($ids)";
		//$sql = "DELETE FROM gtb_pcategory WHERE id_pcategory = " . intval($id);
		$this->db->query($sql);
		return $this->db->affected_rows();
	}

	function getAll( $offset = 0, $number_record = 0, $id_country = 0){
		$result = null;
		$sql = "SELECT p.*, c.vi_name as country_name " .
                       " FROM gtb_province p" .
                       " LEFT JOIN gtb_country c ON p.id_country = c.id" .
                       " WHERE 0 = " .  intval($id_country) . " OR p.id_country = ". intval($id_country) .
                       " ORDER BY p.id DESC".
                       " LIMIT $offset, $number_record";

               $sql = "SELECT p.*, c.vi_name as country_name
                    FROM gtb_province p
                    LEFT JOIN gtb_country c ON p.id_country = c.id
                    WHERE 0 = ". intval($id_country) ." OR p.id_country = ".  intval($id_country)."
                    ORDER BY p.id DESC
                    LIMIT $offset, $number_record";

		$query = $this->db->query($sql);
		$result = $query->result_array();

		return $result;
	}

	function getAllCount($id_country = 0){
		$result = null;
		$sql = "SELECT COUNT(*) as num_record " .
				" FROM gtb_province " .
                        " WHERE 0 = " .  intval($id_country) . " OR id_country = ". intval($id_country);

		$query = $this->db->query($sql);
		$result = $query->result();

		if($result) return $result[0]->num_record;

		return 0;
	}


	function getMaxPosition(){

		$sql = "SELECT MAX(position) as position_max
				FROM gtb_province
                        ";
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0]->position_max;
		}

		return 0;
	}

	function getById($id){

		$sql = "SELECT p.*, c.vi_name as cname
				FROM gtb_province p
                                LEFT JOIN gtb_country c ON c.id = p.id_country
				WHERE p.id = " . intval($id);
		$query = $this->db->query($sql);
		if($result = $query->result_array()){
			return $result[0];
		}

		return null;
	}

        
}

// EOF

